package com.zyk.leetcode;

/**
 * @author zhangsan
 * @date 2021/5/9 16:18
 */
public class C794 {


    public static boolean validTicTacToe(String[] board) {
        int oCnt = 0, xCnt = 0;
        for (String s : board) {
            for (int i = 0; i < 3; i++) {
                if(s.charAt(i) == 'O')
                    oCnt++;
                else if(s.charAt(i) == 'X')
                    xCnt++;
            }
        }

        if(oCnt != xCnt && oCnt != xCnt - 1)            // 若他俩数量不一样, 且O不等X-1的数量
            return false;
        if(win(board, 'X') && oCnt != xCnt - 1)     // 若X赢了, O不必X少一个
            return false;
        if(win(board, 'O') && oCnt != xCnt)         // 若O赢了, X不等O的数量
            return false;
        return true;
    }

    public static boolean win(String[] B, char c) {
        for (int i = 0; i < 3; i++) {
            if(B[i].charAt(0) == c && B[i].charAt(1) == c &&  B[i].charAt(2) == c ) {   // 一行
                return true;
            }
            if(B[0].charAt(i) == c && B[1].charAt(i) == c && B[2].charAt(i) == c) {      // 一列
                return true;
            }
        }
        if(B[0].charAt(0) == c && B[1].charAt(1) == c && B[2].charAt(2) == c)
            return true;
        if(B[0].charAt(2)== c && B[1].charAt(1) == c && B[2].charAt(0) == c)
            return true;
        return false;
    }


    public static void main(String[] args) {
        String[] board = {
                "XXX",
                "   ",
                "OOO"
        };
        System.out.println(validTicTacToe(board));

    }

}
